import request from '@/utils/request'
import * as mock from './mock/order'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取订单列表
 * @param {Object} params 查询参数
 * @param {number} [params.page=1] - 页码
 * @param {number} [params.pageSize=10] - 每页条数
 * @param {string} [params.status] - 订单状态
 * @param {string} [params.keyword] - 搜索关键词
 * @returns {Promise}
 */
export function getOrderList(params) {
  if (isMock) {
    return Promise.resolve(mock.getOrderList(params))
  }
  return request({
    url: '/order/list',
    method: 'get',
    params
  })
}

/**
 * 获取订单详情
 * @param {string} orderId 订单ID
 * @returns {Promise}
 */
export function getOrderDetail(orderId) {
  if (isMock) {
    return Promise.resolve(mock.getOrderDetail(orderId))
  }
  return request({
    url: `/order/detail/${orderId}`,
    method: 'get'
  })
}

/**
 * 更新订单状态
 * @param {Object} data 订单数据
 * @param {string} data.orderId - 订单ID
 * @param {string} data.status - 订单状态
 * @returns {Promise}
 */
// export function updateOrderStatus(data) {
//   if (isMock) {
//     return Promise.resolve(mock.updateOrderStatus(data))
//   }
//   return request({
//     url: '/order/status',
//     method: 'put',
//     data
//   })
// }

/**
 * 取消订单
 * @param {string} orderId 订单ID
 * @param {string} reason 取消原因
 * @returns {Promise}
 */
export function cancelOrder(orderId, reason) {
  if (isMock) {
    return Promise.resolve(mock.cancelOrder(orderId, reason))
  }
  return request({
    url: '/order/cancel',
    method: 'post',
    data: {
      orderId,
      reason
    }
  })
}

/**
 * 确认收货
 * @param {string} orderId 订单ID
 * @returns {Promise}
 */
export function confirmReceived(orderId) {
  if (isMock) {
    return Promise.resolve(mock.confirmReceived(orderId))
  }
  return request({
    url: '/order/confirm',
    method: 'post',
    data: {
      orderId
    }
  })
}

/**
 * 申请售后
 * @param {Object} data 售后数据
 * @param {string} data.orderId - 订单ID
 * @param {string} data.type - 售后类型：1-退货退款，2-仅退款
 * @param {string} data.reason - 售后原因
 * @param {string} data.description - 问题描述
 * @param {Array} data.images - 图片列表
 * @param {string} data.contact - 联系方式
 * @returns {Promise}
 */
export function applyRefund(data) {
  if (isMock) {
    return Promise.resolve(mock.applyRefund(data))
  }
  return request({
    url: '/order/refund',
    method: 'post',
    data
  })
}

/**
 * 删除订单
 * @param {string} orderId 订单ID
 * @returns {Promise}
 */
// export function deleteOrder(orderId) {
//   if (isMock) {
//     return Promise.resolve(mock.deleteOrder ? mock.deleteOrder(orderId) : {
//       success: true,
//       message: '删除成功'
//     })
//   }
//   return request({
//     url: `/order/delete/${orderId}`,
//     method: 'post'
//   })
// }

/**
 * 支付订单
 * @param {Object} data 支付数据
 * @param {string} data.orderId - 订单ID
 * @param {string} data.paymentMethod - 支付方式
 * @returns {Promise}
 */
export function payOrder(data) {
  if (isMock) {
    return Promise.resolve(mock.payOrder ? mock.payOrder(data) : {
      success: true,
      message: '支付成功'
    })
  }
  return request({
    url: '/order/pay',
    method: 'post',
    data
  })
}

/**
 * 创建订单
 * @param {Object} orderData 订单数据
 * @param {string|number} orderData.goodsId 商品ID
 * @param {string|number} orderData.specId 规格ID
 * @param {number} orderData.quantity 购买数量
 * @param {string|number} orderData.addressId 收货地址ID
 * @param {string} orderData.paymentMethod 支付方式
 * @param {string} orderData.remark 订单备注
 * @returns {Promise}
 */
export function createOrder(orderData) {
  if (isMock) {
    return Promise.resolve({
      code: 0,
      msg: '创建订单成功',
      data: {
        orderId: Date.now().toString(),
        orderNo: 'ORD' + Date.now(),
        totalAmount: orderData.totalAmount || '99.00',
        paymentMethod: orderData.paymentMethod,
        createTime: new Date().toISOString()
      }
    })
  }
  return request({
    url: '/order/create',
    method: 'post',
    data: orderData
  })
}

